package com.datahandlers;

import com.gui.helper.CustomTableModel;
import com.inventory.controllers.WeightControllerRemote;
import com.inventory.entities.ItemWeight;
import java.util.ArrayList;
import java.util.List;

/**
 * Handles all the user interface actions for the Item_Weight relation in the
 * database.
 * @author sithum
 */
public class ItemWeightDataHandler implements DataHandler {

    private List<ItemWeight> weightList;
    private Object[][] tableData;
    private WeightControllerRemote controller;
    private String[] weights;
    private List<ItemWeight> data;
    private static int recordCount = 0;

    public void init(Object controller) throws Exception {
        this.controller = (WeightControllerRemote) controller;
        this.weightList = new ArrayList<ItemWeight>();
        recordCount = this.controller.count();
        this.processData();
    }

    public void addRecord(Object object) {
        ItemWeight w = (ItemWeight) object;
        w.setWeightId(++recordCount);
        weightList.add(w);
    }

    public void addEditRecord(Object key, Object categoryName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void editData() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void saveData() throws Exception {
        this.controller.create(weightList);
    }

    public CustomTableModel getModel() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean temporaryStorageEmpty() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean tempEditStorageEmpty() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * Process the data retrived from the database to display in a JTable.
     * @return java.lang.Object[][]
     * @throws java.lang.Exception
     */
    private void processData() throws Exception {
        setData(controller.findAll());
        this.processData(getData());
    }

    private void processData(List<ItemWeight> data) {
        Object[][] dataSet = new Object[data.size()][ItemWeight.class.getDeclaredFields().length];
        String[] w = new String[data.size()];
        int index = 0;
        for (ItemWeight weight : data) {
            String value = weight.getWeight().toString();
            String unit = weight.getUnit();
            w[index] = value.concat(unit);
            index++;
        }
        this.setWeights(w);
    }

    /**
     * @return the data
     */
    public List<ItemWeight> getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List<ItemWeight> data) {
        this.data = data;
    }

    /**
     * @return the weights
     */
    public String[] getWeights() {
        return weights;
    }

    /**
     * @param weights the weights to set
     */
    public void setWeights(String[] weights) {
        this.weights = weights;
    }
}
